package com.bw.bwskinfilecreator.utils;//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//


import java.io.*;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;

public final class FileIOUtils {
    private static int sBufferSize = 524288;

    private FileIOUtils() {
        throw new UnsupportedOperationException("u can't instantiate me...");
    }

    public static boolean writeFileFromIS(String filePath, InputStream is) {
        return writeFileFromIS((File) UtilsBridge.getFileByPath(filePath), is, false, (OnProgressUpdateListener) null);
    }

    public static boolean writeFileFromIS(String filePath, InputStream is, boolean append) {
        return writeFileFromIS((File) UtilsBridge.getFileByPath(filePath), is, append, (OnProgressUpdateListener) null);
    }

    public static boolean writeFileFromIS(File file, InputStream is) {
        return writeFileFromIS((File) file, is, false, (OnProgressUpdateListener) null);
    }

    public static boolean writeFileFromIS(File file, InputStream is, boolean append) {
        return writeFileFromIS((File) file, is, append, (OnProgressUpdateListener) null);
    }

    public static boolean writeFileFromIS(String filePath, InputStream is, OnProgressUpdateListener listener) {
        return writeFileFromIS(UtilsBridge.getFileByPath(filePath), is, false, listener);
    }

    public static boolean writeFileFromIS(String filePath, InputStream is, boolean append, OnProgressUpdateListener listener) {
        return writeFileFromIS(UtilsBridge.getFileByPath(filePath), is, append, listener);
    }

    public static boolean writeFileFromIS(File file, InputStream is, OnProgressUpdateListener listener) {
        return writeFileFromIS(file, is, false, listener);
    }

    public static boolean writeFileFromIS(File file, InputStream is, boolean append, OnProgressUpdateListener listener) {
        if (is != null && UtilsBridge.createOrExistsFile(file)) {
            OutputStream os = null;

            boolean var6;
            try {
                os = new BufferedOutputStream(new FileOutputStream(file, append), sBufferSize);
                if (listener == null) {
                    byte[] data = new byte[sBufferSize];

                    int len;
                    while ((len = is.read(data)) != -1) {
                        os.write(data, 0, len);
                    }
                } else {
                    double totalSize = (double) is.available();
                    int curSize = 0;
                    listener.onProgressUpdate(0.0);
                    byte[] data = new byte[sBufferSize];

                    int len;
                    while ((len = is.read(data)) != -1) {
                        os.write(data, 0, len);
                        curSize += len;
                        listener.onProgressUpdate((double) curSize / totalSize);
                    }
                }

                boolean var25 = true;
                return var25;
            } catch (IOException var22) {
                var22.printStackTrace();
                var6 = false;
            } finally {

                try {
                    is.close();
                } catch (IOException var21) {
                    var21.printStackTrace();
                }

                try {
                    if (os != null) {
                        os.close();
                    }
                } catch (IOException var20) {
                    var20.printStackTrace();
                }

            }

            return var6;
        } else {
            return false;
        }
    }

    public static boolean writeFileFromBytesByStream(String filePath, byte[] bytes) {
        return writeFileFromBytesByStream((File) UtilsBridge.getFileByPath(filePath), bytes, false, (OnProgressUpdateListener) null);
    }

    public static boolean writeFileFromBytesByStream(String filePath, byte[] bytes, boolean append) {
        return writeFileFromBytesByStream((File) UtilsBridge.getFileByPath(filePath), bytes, append, (OnProgressUpdateListener) null);
    }

    public static boolean writeFileFromBytesByStream(File file, byte[] bytes) {
        return writeFileFromBytesByStream((File) file, bytes, false, (OnProgressUpdateListener) null);
    }

    public static boolean writeFileFromBytesByStream(File file, byte[] bytes, boolean append) {
        return writeFileFromBytesByStream((File) file, bytes, append, (OnProgressUpdateListener) null);
    }

    public static boolean writeFileFromBytesByStream(String filePath, byte[] bytes, OnProgressUpdateListener listener) {
        return writeFileFromBytesByStream(UtilsBridge.getFileByPath(filePath), bytes, false, listener);
    }

    public static boolean writeFileFromBytesByStream(String filePath, byte[] bytes, boolean append, OnProgressUpdateListener listener) {
        return writeFileFromBytesByStream(UtilsBridge.getFileByPath(filePath), bytes, append, listener);
    }

    public static boolean writeFileFromBytesByStream(File file, byte[] bytes, OnProgressUpdateListener listener) {
        return writeFileFromBytesByStream(file, bytes, false, listener);
    }

    public static boolean writeFileFromBytesByStream(File file, byte[] bytes, boolean append, OnProgressUpdateListener listener) {
        return bytes == null ? false : writeFileFromIS((File) file, new ByteArrayInputStream(bytes), append, listener);
    }

    public static boolean writeFileFromBytesByChannel(String filePath, byte[] bytes, boolean isForce) {
        return writeFileFromBytesByChannel(UtilsBridge.getFileByPath(filePath), bytes, false, isForce);
    }

    public static boolean writeFileFromBytesByChannel(String filePath, byte[] bytes, boolean append, boolean isForce) {
        return writeFileFromBytesByChannel(UtilsBridge.getFileByPath(filePath), bytes, append, isForce);
    }

    public static boolean writeFileFromBytesByChannel(File file, byte[] bytes, boolean isForce) {
        return writeFileFromBytesByChannel(file, bytes, false, isForce);
    }

    public static boolean writeFileFromBytesByChannel(File file, byte[] bytes, boolean append, boolean isForce) {
        if (bytes == null) {
            return false;
        } else if (!UtilsBridge.createOrExistsFile(file)) {
            return false;
        } else {
            FileChannel fc = null;

            boolean var6;
            try {
                fc = (new FileOutputStream(file, append)).getChannel();
                boolean var5;
                if (fc != null) {
                    fc.position(fc.size());
                    fc.write(ByteBuffer.wrap(bytes));
                    if (isForce) {
                        fc.force(true);
                    }

                    var5 = true;
                    return var5;
                }

                var5 = false;
                return var5;
            } catch (IOException var17) {
                var17.printStackTrace();
                var6 = false;
            } finally {
                try {
                    if (fc != null) {
                        fc.close();
                    }
                } catch (IOException var16) {
                    var16.printStackTrace();
                }

            }

            return var6;
        }
    }

    public static boolean writeFileFromBytesByMap(String filePath, byte[] bytes, boolean isForce) {
        return writeFileFromBytesByMap(filePath, bytes, false, isForce);
    }

    public static boolean writeFileFromBytesByMap(String filePath, byte[] bytes, boolean append, boolean isForce) {
        return writeFileFromBytesByMap(UtilsBridge.getFileByPath(filePath), bytes, append, isForce);
    }

    public static boolean writeFileFromBytesByMap(File file, byte[] bytes, boolean isForce) {
        return writeFileFromBytesByMap(file, bytes, false, isForce);
    }

    public static boolean writeFileFromBytesByMap(File file, byte[] bytes, boolean append, boolean isForce) {
        if (bytes != null && UtilsBridge.createOrExistsFile(file)) {
            FileChannel fc = null;

            boolean var6;
            try {
                fc = (new FileOutputStream(file, append)).getChannel();
                if (fc == null) {
                    boolean var19 = false;
                    return var19;
                }

                MappedByteBuffer mbb = fc.map(MapMode.READ_WRITE, fc.size(), (long) bytes.length);
                mbb.put(bytes);
                if (isForce) {
                    mbb.force();
                }

                var6 = true;
            } catch (IOException var17) {
                var17.printStackTrace();
                var6 = false;
                return var6;
            } finally {
                try {
                    if (fc != null) {
                        fc.close();
                    }
                } catch (IOException var16) {
                    var16.printStackTrace();
                }

            }

            return var6;
        } else {
            return false;
        }
    }

    public static boolean writeFileFromString(String filePath, String content) {
        return writeFileFromString(UtilsBridge.getFileByPath(filePath), content, false);
    }

    public static boolean writeFileFromString(String filePath, String content, boolean append) {
        return writeFileFromString(UtilsBridge.getFileByPath(filePath), content, append);
    }

    public static boolean writeFileFromString(File file, String content) {
        return writeFileFromString(file, content, false);
    }

    public static boolean writeFileFromString(File file, String content, boolean append) {
        if (file != null && content != null) {
            if (!UtilsBridge.createOrExistsFile(file)) {
                return false;
            } else {
                BufferedWriter bw = null;

                boolean var5;
                try {
                    bw = new BufferedWriter(new FileWriter(file, append));
                    bw.write(content);
                    boolean var4 = true;
                    return var4;
                } catch (IOException var15) {
                    var15.printStackTrace();
                    var5 = false;
                } finally {
                    try {
                        if (bw != null) {
                            bw.close();
                        }
                    } catch (IOException var14) {
                        var14.printStackTrace();
                    }

                }

                return var5;
            }
        } else {
            return false;
        }
    }

    public interface OnProgressUpdateListener {
        void onProgressUpdate(double var1);
    }
}
